const exp = require('express');
const data = require('./data/newdata.json')
const app = exp();
app.listen(80,()=>{
    console.log("启动成功");
})

app.get('/',(req,res)=>{
    res.redirect('/list');
})


app.get('/list',(req,res)=>{

    // let str = '';

    // data.forEach(item=>{
    //     str+=`<h3>${item.newsTitle}</h3>`;
    // })

    let arr = data.map(item=>{
        return `<h3><a href="/detail/${item.id}.html">${item.newsTitle}</a></h3>`;
    });
    let str = arr.join('');
    console.log(arr);
    res.send(str);
});


app.get('/detail/:id.html',(req,res)=>{

    // console.log(req.params.id);

    let {newsTitle,newsContent} = data.find(item=>item.id === req.params.id);

    let str = `
        <h3>${newsTitle}</h3>
        <p>${newsContent}</p>
    `;

    res.send(str);
})